package com.cos.shop.web.filter; /**
 * @author: COS
 * @time: 2023/5/11 9:35
 * @description:
 */

import com.cos.shop.common.Const;
import com.cos.shop.common.ResponseCode;
import com.cos.shop.common.ResponseResult;
import com.cos.shop.entity.User;
import com.cos.shop.services.UserServices;
import com.cos.shop.services.impl.UserServicesImpl;
import com.cos.shop.util.Base64Utils;

import javax.servlet.*;
import javax.servlet.annotation.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: COS
 * @time: 2023/5/11 9:36
 * @description: 处理自动登录
 */
@WebFilter(filterName = "Filter02_AutoLogin", value = "/login.jsp")
public class Filter02_AutoLogin implements Filter {
    public void init(FilterConfig config) throws ServletException {
    }

    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse resp = (HttpServletResponse) response;
        //1.判断用户是否已有登录
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute(Const.LOGIN_INFO);
        if (user != null) {
            //2.已有登录,设置msg已有登录message.jsp
            //用户一登录设置信息提示转消息页面
            req.setAttribute(Const.MSG, ResponseResult.createFail("已有登录,请进入首页"));
            req.getRequestDispatcher("message.jsp").forward(req, resp);
        } else {
            //3.没有登录,做自动登录的判断
            //4.在cookie获取用户名和密码
            String autoLoginInfo=null;
            for (Cookie cookie : req.getCookies()) {
                // 找到自动登录的数据
                if (cookie.getName().equals(Const.AUTO_KEY)) {
                    //获取自动登录的数据
                    autoLoginInfo= cookie.getValue();
                }
            }
            //5.判断是否存在用户名和密码
            if (autoLoginInfo == null) {
                //6.如果不存在,直接放行,用户自己进入登录页登录
                chain.doFilter(req, resp);
            }else{
                //7.存在用户名和密码,做自动登录
                // 提取用户名和密码   用户名:密码
                String decode = Base64Utils.decode(autoLoginInfo);
                String username = decode.split(":")[0];
                String password = decode.split(":")[1];
                //8.把用户名和密码进行解码 调用登录业务,处理登录
                UserServices us = new UserServicesImpl();
                ResponseResult responseResult = us.doLogin(username, password);
                if (responseResult.getCode()== ResponseCode.SUCCESS.getCode()) {
                    //9.验证成功完成自动登录
                    session.setAttribute(Const.LOGIN_INFO,responseResult.getT());
                    //重定向到首页
                    resp.sendRedirect("/index.jsp");
                }else {
                    //10.验证失败,清空自动登录 cookie,放行用户自动登录
                    req.setAttribute(Const.MSG,responseResult);
                    req.getRequestDispatcher("message.jsp").forward(req,resp);
                }
            }
        }
    }
}
